#ifndef WEBSOCKETSERVER_H
#define WEBSOCKETSERVER_H
#include <cstddef>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <cctype>
#include <atomic>
#include <thread>
#include <mutex>
#include <set>
#include <queue>
#include <condition_variable>
#include <unistd.h>       // close()
#include <sys/socket.h>   // socket(), bind(), listen(), accept()
#include <netinet/in.h>   // sockaddr_in, INADDR_ANY, htons()
#include <arpa/inet.h>    // inet_ntoa() (如果用到 IP 地址转换)
#include <cstring>        // strstr(), memset()
#include <openssl/sha.h>  // SHA1()
#include <openssl/bio.h>  // BIO 相关函数 (Base64 编码)
#include <openssl/evp.h>  // EVP 编码 (Base64)
#include <openssl/buffer.h>  // 提供 BUF_MEM 的完整定义
struct LogMessage {
    std::string client_ip;
    std::string method;
    std::string url;
    std::string result;
    int port;
    std::string timestamp;
};

// 防火墙配置消息类型
struct FirewallConfigMessage {
    std::vector<int> blocked_ports;
    int syn_flood_threshold;
    int syn_flood_ban_time;
    int slow_dos_timeout;
    int slow_dos_min_rate;
};

// 全局日志队列
extern std::queue<LogMessage> log_queue;
extern std::mutex queue_mutex;
extern std::condition_variable queue_cv;
extern std::atomic<bool> log_thread_running;

// 日志处理线程函数
class WebSocketServer {
public:
    WebSocketServer(int port);
    ~WebSocketServer();
    void start();
    void stop();
    void broadcast(const LogMessage& message);
    
private:
    void run();
    void handleClient(int client_fd);
    
    int server_fd;
    int port;
    std::atomic<bool> running;
    std::thread server_thread;
    std::mutex clients_mutex;
    std::set<int> clients;
};
void log_processor_thread(WebSocketServer* ws_server);
// 从客户端套接字接收并解析HTTP请求
#endif